﻿using CoachingOrganizer.iOS.Controllers.Tools.MoneyCalculation;
using CoachingOrganizer.iOS.Decorators;
using CoachingOrganizer.iOS.Helpers;
using CoachingOrganizer.Logic.Models.Tools.MoneyCalculation;
using MonoTouch.UIKit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CoachingOrganizer.iOS.DataSources.Tools.MoneyCalculation
{
    public class StableSpentsDataSource : UITableViewSource
    {
        public const string CellIdentifer = "StableSpentsItemCell";

        private readonly UITableView _view;
        private readonly StableSpentsMoneyCalculationDetailsViewController _controller;
        private readonly MoneyCalculationModel _model;
        private readonly SimpleTableDataSourceDecorator<StableSpentsItemViewController, StableMoneyFlowItemModel> _tableCellsSource;

        public StableSpentsDataSource(UITableView view, StableSpentsMoneyCalculationDetailsViewController controller, MoneyCalculationModel model)
        {
            _view = view;
            _controller = controller;
            _model = model;
            _tableCellsSource = new SimpleTableDataSourceDecorator<StableSpentsItemViewController, StableMoneyFlowItemModel>(CellIdentifer, _view, _controller, p => _model.Spents.StableItems.ElementAtOrDefault(p.Row), m => new StableSpentsItemViewController(m));
            model.Spents.StableMoneyFlowItemsCollectionChanged += Spents_StableMoneyFlowItemsCollectionChanged;
        }

        public override UITableViewCell GetCell(UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
        {
            return _tableCellsSource.GetCell(indexPath);
        }

        public override int RowsInSection(UITableView tableview, int section)
        {
            return UIHelper.GetCollectionItemsCount(_model.Spents.StableItems);
        }

        private void Spents_StableMoneyFlowItemsCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            _tableCellsSource.ProceedCollectionChanged(e);
        }
    }
}
